package com.etooling.framework.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import javax.servlet.http.HttpServletResponse;
import java.util.Hashtable;

/**
 * @author 周志荣
 * @title: QrCodeUtils
 * @projectName ems
 * @description: 二维码工具类
 * @date 2022/2/1410:58
 */
public class QrCodeUtils {
    public static void createQrCodeByWeb(String content, String desc, String logoWebPath, HttpServletResponse response) {
        //二维码宽度
        int width = 300;
        //二维码高度
        int height = 300;
        //定义二维码图片格式
        String format = "png";
        //二维码内容

        //定义二维码属性
        Hashtable<EncodeHintType, String> his = new Hashtable<EncodeHintType, String>();
        //二维码内容的编码格式
        his.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //定义二维码边距
        his.put(EncodeHintType.MARGIN, "1");

        try {
            //创建位矩阵对象并且生成二维码对应的位矩阵对象
            //BarcodeFormat.QR_CODE  生成图片类型为QRCode
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
//            //二维码生成路径
//            String path = "D:\\Downloads\\jobs\\zxing";
//            //二维码生成名字
//            String name = "myBlog.png";

//            Path path2 = Paths.get(path, name);
            //生成二维码
            MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());
//            BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
//            bufferedImage=insertWords(bufferedImage,desc);
            System.out.println("二维码生成成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
